<template>
  <div class="my-invoice-detail">  
    <row title="店铺名称" >
        <input class="ipt" type="text" slot="rowRight" placeholder="请输入店铺名称" v-model="shopName">
    </row>
    <group>
        <x-address  title="所在区域"
                    :raw-value="true" 
                    :value="value"
                    :list="addressData"  
                    @on-shadow-change="getValue"
                    placeholder="请选择地址">
        </x-address>
    </group>
    
    <row title="店铺地址">
        <input class="ipt" type="text" slot="rowRight" placeholder="请输入店铺地址" v-model="shopAdress">
    </row>
    <row title="联系人">
        <input class="ipt" type="text" slot="rowRight" placeholder="请输入联系人" v-model="cancat">
    </row>
    <row title="联系电话" >
        <input class="ipt" type="number" slot="rowRight" placeholder="请输入联系电话" v-model="cancatPhone">
    </row>
    <row title="管理仓库" placeholder="请选择仓库" isclick :value="warehouseList[0] ? warehouseList[0].name : ''" @click.native="chooseWareHouse()"></row>
    <div class="invoice-photo">
        <div class="map-title">营业执照</div>
        <!-- 上传营业执照 文件 --> 
        <div class="wrap-box">
            <div class="wrpa-item" v-for="(item,index) in licensePics" :key="index">
                <div class="warp-img-box" >
                    <img    class="warp-img" :src="item"  alt="" />
                    <div class="iconfont iconguanbi " @click="delThis('1',index)"></div>
                </div>
            </div>
            <div class="uploader-box"  v-if="licensePics.length<9">
                <div class="add" @click="chooseType('1')">
                    <div class="add-image" align="center">
                        <i class="iconfont iconxiangji"></i>
                        <p class="font13">添加图片</p>
                    </div>
                </div>
            </div>  
        </div>
        
    </div>
    <div class="invoice-photo">
        <div class="map-title">设备二维码</div>
        <!-- 上传文件 -->
        <!-- 上传设备二维码 start --> 
        <div class="wrap-box">
            <div class="wrpa-item" v-for="(item,index) in deviceQrpics" :key="index">
                <div class="warp-img-box" >
                    <img    class="warp-img" :src="item"  alt="" />
                    <div class="iconfont iconguanbi " @click="delThis('2',index)"></div>
                </div>
            </div>
            <div class="uploader-box"  v-if="deviceQrpics.length<9">
                <div class="add" @click="chooseType('2')">
                    <div class="add-image" align="center">
                        <i class="iconfont iconxiangji"></i>
                        <p class="font13">添加图片</p>
                    </div>
                </div>
            </div>  
        </div>
        <!-- 上传设备二维码 end --> 
    </div>
    <div class="invoice-photo">
        <div class="map-title">店铺照片</div>

        <!-- 上传店铺照片 start --> 
        <div class="wrap-box">
            <div class="wrpa-item" v-for="(item,index) in storePics" :key="index">
                <div class="warp-img-box" >
                    <img    class="warp-img" :src="item"  alt="" />
                    <div class="iconfont iconguanbi " @click="delThis('3',index)"></div>
                </div>
            </div>
            <div class="uploader-box" v-if="storePics.length<9">
                <div class="add" @click="chooseType('3')">
                    <div class="add-image" align="center">
                        <i class="iconfont iconxiangji"></i>
                        <p class="font13">添加图片</p>
                    </div>
                </div>
            </div>  
        </div>
        <!-- 上传店铺照片 end --> 
    </div>
    <input @change="fileChange($event)" type="file" ref="inputer" id="upload_file" multiple style="display: none"/>
    <div class="height120"></div>
    <div class="btn isbtn" @click="submit()">提交</div>
  </div>
</template>

<script>
import row from '@/components/row'
import api from '@/api/api.js' 
import { XAddress,Group,ChinaAddressV4Data } from 'vux'
import { Message } from 'element-ui'
export default { 
  components: {
    row, 
    Group,
    XAddress
  },
  data () {
    return {
      value:[],
      addressData: ChinaAddressV4Data,
      warehouseList: this.$store.state.warehouseList ,
      shopName:'',
      shopAdress:'',
      cancat:'',
      cancatPhone:'',
      chooseIdx:'',
      storePics:[],
      licensePics:[],
      deviceQrpics:[],
    }
  },
  created(){  
  },
  mounted () { 
  },
  filters: { 
  }, 
  activated(){ 
    this.warehouseList = this.$store.state.warehouseList; 
  },
  beforeRouteEnter(to, from, next) {
   next(vm=>{  
      if(from.name=="ShopMng"){
        vm.initPage();
      }

    })
  }, 
  methods: {
    initPage(){
      this.value=[];  
      this.shopName='';
      this.shopAdress='';
      this.cancat='';
      this.cancatPhone='';
      this.chooseIdx='';
      this.storePics=[];
      this.licensePics=[];
      this.deviceQrpics=[]; 
      this.$store.commit("setWarehouseList",[]);
    },
    chooseWareHouse(){
      this.$router.push({path:'/chooseWarehouse'})
    },
    getValue(ids,names) {
        this.value =names;
    },
    submit(){
        if(!this.shopName){
            return  Message({
                message: "请输入店铺名！",
                type: 'error',
                duration: 1 * 1000
            })
        }
        if(!this.value[0]){
            return  Message({
                message: "请选择区域",
                type: 'error',
                duration: 1 * 1000
            })
        }
        if(!this.shopAdress){
            return  Message({
                message: "请填写地址",
                type: 'error',
                duration: 1 * 1000
            })
        }
        api.shopMng.shopAdd({
            "storeName":this.shopName,
            "contacts":this.cancat,
            "telephone":this.cancatPhone,
            "province":this.value[0],
            "city":this.value[1],
            "district":this.value[2],
            "storePics":this.storePics.join(","),
            "licensePics":this.licensePics.join(","),
            "deviceQrpics":this.deviceQrpics.join(","),
            "address":this.shopAdress,
            replWarehouseId:this.warehouseList[0] ? this.warehouseList[0].id : ''
        },res=>{
            this.$router.go(-1);
            return  Message({
                message: "添加成功",
                type: 'success',
                duration: 1 * 1000
            })

        })
    },
    //delThis 删除某一项的照片
    delThis(idx,index){
        if(idx==1){ //营业执照
            this.licensePics.splice(index,1)
        }else if(idx==2){
            this.deviceQrpics.splice(index,1)
        }else if(idx==3){
            this.storePics.splice(index,1)
        }
    }, 
    //上传照片
    chooseType(idx) {
        this.chooseIdx=idx;
        document.getElementById('upload_file').click();
    },
    fileChange(el) {
        let file = this.$refs.inputer.files; 
        api.publick.uploadFilePost({files:file},
            res=>{
                if(res.data.resultCode==0){ 
                    if(this.chooseIdx==1){//营业执照
                        this.licensePics.push(res.data.result);
                    }else if(this.chooseIdx==2){//设备二维码
                        this.deviceQrpics.push(res.data.result);
                    }else if(this.chooseIdx==3){//店铺照片
                        this.storePics.push(res.data.result);
                    }
                }
            }
        )
        
    },
  }
}
</script>
<style lang="less" scoped>
/deep/.vux-no-group-title{
    margin-top: 0;
}
/deep/.weui-cells:before{
    display: none;
}
/deep/.weui-cells::after{
    left: 20px;
}
.wrap-box{
    display: flex;
    flex-wrap: wrap;
    .warp-img{
        width: 80px;
        height: 80px;
    }
    .iconguanbi{
        color: #fbfbfb;
        background: rgba(33,33,33,.7);
        font-size: .36rem;
        top: 2px;
        right: 12px;
        position: absolute;
    }
}
.warp-img-box{
    position: relative;
}
.uploader-box{
    width: 100px;
    height: 100px;
}
.invoice-photo{
    padding: 12px 16px;
}
.map-title{
    height: 40px;
    line-height: 40px;
    font-size: 16px;
    font-weight: bold;
}
.invoice-photo-img{
   width: 100%;
   min-width: 74px;
   min-height: 74px; 
}
input {
    outline: none;
}
.ipt{
    border: none;
}
.invoice-order{
    display: flex;
    height: 40px;
    line-height: 40px;

}
.invoice-number{
    color: #444;
    width: 35%;
    padding-left: 8px;
}
.tips{
    color: #999;
    font-size: 11px;
    margin-left: 8px;
}
.invoice-money{
    color: #999;
    width: 30%;
}
.invoice-timer{
   color: #666; 
   width: 35%;
}
.invoice-active{
    background: #e1e1e1;
}

.wrap-box{
    display: flex;
    flex-wrap: wrap;
    img{
        margin-bottom: 10px;
        margin-right: 10px;
        width: 74px;
        height: 74px;    
    }
}
.height120{
    height: 60px;
}
.btn{
    text-align: center;
    line-height: 50px;
    background: #FABE00;
    color: #fff;
    height: 50px;
    font-size: 16px;
    letter-spacing: 4px;
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
}
.add-img {
    width: 100%;
    padding: 10px;
}

.add-img p {
    color: #999;
}
.add-image {
    padding-top: 15px; 
    width: 80px; 
    height: 80px;
    border: 1px dashed rgba(0, 0, 0, .2);
}

.add-image .iconxiangji {
    font-size: 24px;
}
</style>
